package com.mysql.helpers;




import java.sql.Connection;

import com.mysql.entity.Staffs;
import com.mysql.utils.DBUtil;


import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;

/**
 * 作者:wmx  2025-02-07 22:22
 * 1、功能；数据库访问层
 * 2、实现:实现与数据库
 * 3、方法:{
 *     executeDatabaseCount(PreparedStatement preparedStatement sql语句) 对数据进行统计
 *     executeDatabaseUpdate(PreparedStatement preparedStatement sql语句) 对数据进行删除、修改
 * }
 **/
public class DBHelper {
    private  static Connection connection = null;



    public  int executeDatabaseCount(PreparedStatement preparedStatement){
        int num = -1;
        ResultSet resultSet = null;
        try {
            resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                num = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return  num;
        }finally {
            try {
                    DBUtil.getClose(null,resultSet,null);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  num;
    }

    public  int executeDatabaseUpdate(PreparedStatement preparedStatement){
        int num = -1;
        try {
            num = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            return  num;
        }
        return  num;
    }
    public ArrayList<Staffs> executeDatabase(PreparedStatement preparedStatement){
        ArrayList<Staffs> list = new ArrayList<Staffs>();
        ResultSet resultSet = null;
        try {
             resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){


                Integer Id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                int age = resultSet.getInt("age");
                String phone = resultSet.getString("phone");
                String staPos = resultSet.getString("sta_pos");
                Date addTime = resultSet.getDate("add_time");
                Date updateTime = resultSet.getDate("update_time");

                list.add(new Staffs(Id,name,age,phone,staPos,addTime,updateTime));
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return  null;
        }finally {
            try {
                DBUtil.getClose(null,resultSet,null);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return list;
    }



}
